$(function(){
	
	var options = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
 
        dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type) 
        clearForm: false        // clear all form fields after successful submit 

    }; 
 

    $('#form_task').submit(function() { 

        $(this).ajaxSubmit(options); 

        return false; 
    });
    
    $('#btnCancel').click(function(){
    	
    	location.href = location.href;
    	
    });
    
    //选择手续费计算
    var $commission_type = $('input[name="commission_type"]');
    $commission_type.click(function(){
    	
    	var id = $(this).attr('id');
    	
    	if(id == 'commission_danjia'){
    		$('#sp_bili').hide();
    		$('#sp_danjia').show();
    	}else if(id == 'commission_bili'){
    		$('#sp_danjia').hide();
    		$('#sp_bili').show();
    	}
    });
    
    //手续费按单价计算
    var $danjia = $('#danjia');
    //比例
    var $bili = $('#bili');
    //比例
    var $kuaidi = $('#kuaidi');
    
    //手续费
    var $commission_money = $('#commission_money');
    //订单数量
    var $sp_order_count = $('#sp_order_count');
    //代付费
    var $task_order_money = $('#task_order_money');
    
    //增值服务，快递
    var $value_added_money = $('#value_added_money');
    
    
    //单价
    $danjia.change(function(){
    	var danjia = parseFloat($.trim($danjia.val()));
    	
    	if(isNaN(danjia)){
    		alert('手续费,元/单  填写不正确，请重新填写');
    		return false;
    	}
    	
    	if(danjia < 0){
    		alert('手续费,元/单  不得小于0');
    		return false;
    	}
    	
    	var order_count = parseInt($sp_order_count.html());
    	
    	var money = (danjia * order_count).toFixed(2);
    	
    	$commission_money.val(money);
    });
   
    
    //比例
    $bili.change(function(){
    	
    	var bili 		= parseFloat($.trim($bili.val()));
    	
    	if(isNaN(bili)){
    		alert('手续费比例,填写不正确，请重新填写');
    		return false;
    	}
    	
    	if(bili < 0){
    		alert('手续费比例,不得小于0');
    		return false;
    	}
    	
    	var task_order_money = parseFloat($task_order_money.val());
    	
    	var money = (task_order_money * bili / 100).toFixed(2);
    	
    	$commission_money.val(money);
    });
    
    
    //快递
    $kuaidi.change(function(){
    	var kuaidi = parseFloat($.trim($kuaidi.val()));
    	
    	if(isNaN(kuaidi)){
    		alert('快递信息,元/单  填写不正确，请重新填写');
    		return false;
    	}
    	
    	if(kuaidi < 0){
    		alert('快递信息,元/单  不得小于0');
    		return false;
    	}
    	
    	var order_count = parseInt($sp_order_count.html());
    	
    	var money = (kuaidi * order_count).toFixed(2);
    	
    	$value_added_money.val(money);
    });
    
    
    //上传文件
    var $btnUpload = $('#btnUpload');
    $btnUpload.click(function(){
    	
    	//记录选的客户和时间
    	$('#hd_user_id').val($('#user_id').val());
    	$('#hd_begin_time').val($('#begin').val());
		$('#hd_end_time').val($('#end').val());
		
    	$('#form_file').submit();
    });
    
    
    //统计总金额
    $('#task_order_money,#commission_money,#value_added_money,#danjia,#bili,#kuaidi').change(function(){
    
    	var task_order_money 	= parseFloat($('#task_order_money').val());
    	var commission_money 	= parseFloat($('#commission_money').val());
    	var value_added_money 	= parseFloat($('#value_added_money').val());
    	
    	if(isNaN(task_order_money)){
    		task_order_money = 0;
    	}
    	if(isNaN(commission_money)){
    		commission_money = 0;
    	}
    	if(isNaN(value_added_money)){
    		value_added_money = 0;
    	}
    	
    	var total_money = task_order_money + commission_money + value_added_money;
    	
    	$('#sp_total_money').html(total_money);
    });
    
    
});


function showRequest(formData, jqForm, options) { 
    //var queryString = $.param(formData); 
    return true; 
} 

function showResponse(data, statusText, xhr, $form)  { 
	var form_id = $form.attr('id');

	if(data.success){
		alert(data.message);
				
		if(form_id == 'form_task'){
			location.href = '/index.php/Admin/Task/';
		}
		
	}else{
		alert(data.message);
	}
}